package com.rocket.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.rocket.R;
import com.rocket.database.WorkSql;
import com.rocket.entity.FirearmType;
import com.rocket.entity.Work;
import com.rocket.excel.WorkExcelUtil;
import com.rocket.util.DialogUtil;
import com.rocket.util.MenuUtil;

public class HistoryListActivity extends MenuActivity {
	
	ArrayList<Work> mWorks;
	Button buttonBack;
	Button buttonPrint;

	ListView mListView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.historylist);
		
		String name = getIntent().getStringExtra("name");
		long start = getIntent().getLongExtra("start", 0);
		long end = getIntent().getLongExtra("end", 0);
		ArrayList<Integer> types = getIntent().getIntegerArrayListExtra("types");
		
		mWorks = WorkSql.getInstance().query(name,start,end,types);
		

		buttonBack = (Button) findViewById(R.id.buttonback);
		buttonBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		buttonPrint = (Button)findViewById(R.id.buttonprint);
		buttonPrint.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Rocket/work_" + System.currentTimeMillis() + ".xls";
				WorkExcelUtil.getInstance().export(mWorks,path);
				showToast("Excel文件保存成功，文件路径为："+path);
			}
		});
		
		mListView = (ListView)findViewById(R.id.listview);
		mListView.setAdapter(new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if(convertView == null){
					convertView = getLayoutInflater().inflate(R.layout.historylistitem, null);
				}
				TextView tvName = (TextView)convertView.findViewById(R.id.tvname);
				tvName.setText(mWorks.get(position).name);
				
				TextView tvType = (TextView)convertView.findViewById(R.id.tvtype);
				int type = mWorks.get(position).firearmType;
				switch (type) {
				case FirearmType.ROCKET_BL1:
					tvType.setText("BL-1型火箭弹");
					break;
				case FirearmType.ROCKET_HJD82:
					tvType.setText("HJD-82型火箭弹");
					break;
				case FirearmType.ROCKET_RY6300:
					tvType.setText("RYI-6300型火箭弹");
					break;
				case FirearmType.ROCKET_WR98:
					tvType.setText("WR-98型火箭弹");
					break;
				case FirearmType.MISSILE_83:
					tvType.setText("83型炮弹");
					break;
				case FirearmType.MISSILE_JD89:
					tvType.setText("JD-89型炮弹");
					break;
				default:
					break;
				}
				
				TextView tvTime = (TextView)convertView.findViewById(R.id.tvtime);
				long time = mWorks.get(position).createTime;
				SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
				tvTime.setText(format.format(new Date(time)));
				
				return convertView;
			}
			
			@Override
			public long getItemId(int position) {
				return position;
			}
			
			@Override
			public Object getItem(int position) {
				return mWorks.get(position);
			}
			
			@Override
			public int getCount() {
				return mWorks.size();
			}
		});
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Work work = mWorks.get(arg2);
				Intent intent = new Intent(HistoryListActivity.this,WorkInfoActivity.class);
				intent.putExtra("work", work);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void openMenu() {
		String[] texts = new String[] { "火器选择", "添加站点", "历史查询", "退出程序" };
		Integer[] images = new Integer[] { R.drawable.menu_logout,
				R.drawable.menu_sys_add_buddy,
				R.drawable.menu_sys_search_buddy, R.drawable.menu_icon };
		mMenu = new MenuUtil(HistoryListActivity.this, getLayoutInflater()
				.inflate(R.layout.historyquery, null), texts, images);
		mMenu.setMenuItemSelectedLinstener(new OnClickListener() {

			public void onClick(View v) {
				int tag = (Integer) v.getTag();
				switch (tag) {
				case 0:
					mMenu.dismiss();
					Intent intent1 = new Intent(HistoryListActivity.this,
							ChoseFirearmActivity.class);
					intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent1);
					break;
				case 1:
					mMenu.dismiss();
					Intent intent2 = new Intent(HistoryListActivity.this,
							WorkStationAddActivity.class);
					intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent2);
					break;
				case 2:
					mMenu.dismiss();
					Intent intent3 = new Intent(HistoryListActivity.this,
							HistoryListActivity.class);
					intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent3);
					break;
				case 3:
					mMenu.dismiss();
					mDialog = new DialogUtil(HistoryListActivity.this);
					mDialog.setTitle("退出确认");
					mDialog.setMessage("确定退出吗？");
					mDialog.setPositiveButton("确定", new OnClickListener() {

						public void onClick(View v) {
							mDialog.dismiss();
							Intent intent = new Intent(
									HistoryListActivity.this,
									MainActivity.class);
							intent.putExtra("Flag", 1);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(intent);
						}
					});
					mDialog.setNegativeButton("取消", null);
					mDialog.show();
					break;
				default:
					break;
				}
			}
		});
		mMenu.show();
	}

}
